package com.th.therp.controller;

import com.th.therp.exception.BaseException;
import com.th.therp.pojo.dto.SaleDto;
import com.th.therp.pojo.dto.page.SalePageQueryDTO;
import com.th.therp.pojo.result.PageResult;
import com.th.therp.pojo.result.Result;
import com.th.therp.service.SaleService;
import jakarta.validation.Valid;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

/**
 * 销售单请求控制器
 */
@Log4j2
@RestController
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    private SaleService saleService;

    @PostMapping()
    @ResponseBody
    public Result add(@Valid @RequestBody SaleDto saleDto, BindingResult bindingResult){
        log.info("新增销售单:{}",saleDto);
        if (bindingResult.hasErrors()){
            throw new BaseException("格式不正确");
        }
        saleService.add(saleDto);
        return Result.success();
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<PageResult> page(SalePageQueryDTO salePageQueryDTO){
        log.info("销售单分页查询:{}",salePageQueryDTO);
        PageResult pageResult = saleService.page(salePageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping()
    @ResponseBody
    public Result delete(@RequestParam Integer id){
        log.info("销售单拖货:{}",id);
        saleService.delete(id);
        return Result.success();
    }
}